//
//  ViewController.m
//  masonry18
//
//  Created by Color_Day on 15/8/18.
//  Copyright (c) 2015年 kad. All rights reserved.
//

#import "ViewController.h"




@interface ViewController () <UIScrollViewDelegate>
/**数据源*/
@property(nonatomic,strong)NSMutableArray * dataArr;

@property (nonatomic, strong) NSMutableArray * PlistViewArr;
/**头部的View*/
@property(nonatomic,strong)NSMutableArray *topArr;
@end

@implementation ViewController
{
    UIScrollView * scro;
    NSInteger _lastPosition ;
    
}

-(NSMutableArray *)dataArr
{
    if (_dataArr == nil) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    NSString * strPath = [[NSBundle mainBundle]pathForResource:@"ios.ceshi" ofType:nil];
    
    
    NSData * datapath = [NSData dataWithContentsOfFile:strPath];
     NSDictionary * arrDic = [NSJSONSerialization JSONObjectWithData:datapath options:NSJSONReadingMutableLeaves error:nil];
    
    _dataArr  = [NSMutableArray arrayWithObject:[ChyModel objectWithKeyValues:arrDic]];
    
    
    return _dataArr;
}

-(NSMutableArray *)PlistViewArr{
    if ( _PlistViewArr == nil) {
        _PlistViewArr = [[NSMutableArray alloc]init];
    }
    return _PlistViewArr;
}
-(NSMutableArray *)topArr
{
    if (_topArr == nil) {
        _topArr = [[NSMutableArray alloc]init];
    }
    return _topArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor chycolorR:238 andG:238 andB:238];
    _dataArr = [self dataArr];
    
    _PlistViewArr = [self PlistViewArr];
    
    _topArr = [self topArr];
   
    
    //    添加UI
    [self addSetUI];
     NSLog(@"_topArr %@", _topArr);
    //
    //    //   添加数据
    [self SetAddData];
    
}
///创建UI添加masonry
-(void)addSetUI{
    
    UIImageView * imageone = [self addimageView:@"BgImage_image"];
    [self.view addSubview:imageone];
    //    scro
    scro = [[UIScrollView alloc]init];
    scro.delegate = self;
    [self.view addSubview:scro];
    //    最上面一层View
    UIView * contenView = [[UIView alloc ]init];
    [scro addSubview:contenView];
    
    
    
    UIView * topView = [[UIView alloc]init];
    topView.tag = 10;
    
    [contenView addSubview:topView];
    [_topArr addObject:topView];
    
    
    UIButton  * loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.backgroundColor = [UIColor redColor];
    [topView addSubview:loginBtn];
    
    
    UIImageView * piciamge = [[UIImageView alloc]init];
    piciamge.tag = 1000;
    piciamge.hidden = YES;
    [topView addSubview:piciamge];
    
    UILabel * namelabel = [self addtextLabel:@""];
    namelabel.tag = 1001;
    namelabel.hidden = YES;
    namelabel.textColor = [UIColor chycolorR:255 andG:255 andB:255];
    [topView addSubview:namelabel];
    UILabel * jifen = [self addtextLabel:@""];
    jifen.tag = 1002;
    jifen.hidden = YES;
    jifen.textColor = [UIColor chycolorR:255 andG:255 andB:255];
    jifen.textAlignment = NSTextAlignmentCenter;
    [topView addSubview: jifen];
 
    UIView * dingdanView = [[UIView alloc]init];
    dingdanView.backgroundColor = [UIColor chycolorR:255 andG:255 andB:255];
    [contenView addSubview:dingdanView];
    
    UIView * bgView = [[UIView alloc]init];
    bgView.tag = 11;
    [_topArr addObject:bgView];
    bgView.backgroundColor = [UIColor chycolorR:238 andG:238 andB:238];
    [contenView addSubview:bgView];
    
    
    

//    settingButton
    UIButton * settingbtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [settingbtn setTitle:@"设置" forState:UIControlStateNormal];
    [settingbtn setTitleColor:[UIColor chycolorR:255 andG:255 andB:255] forState:UIControlStateNormal];
    [settingbtn addTarget:self  action:@selector(settingbtn:) forControlEvents:(UIControlEventTouchUpInside)];
    [topView addSubview:settingbtn];
    
    
    
    
    
    
    
    
    
    
    
    
    //    PlistView
    UIView * plistView = [[UIView alloc]init];
    plistView.backgroundColor = [UIColor chycolorR:255 andG:255 andB:255];
    [contenView addSubview:plistView];
    
    
#pragma mark ===========添加约束
    
    [scro mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    [imageone mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(300);
        make.top.mas_equalTo(0);
        make.left.and.right.offset(0);
    }];
    
    
    //    contenView
    [contenView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scro);
        make.width.equalTo(scro);
    }];
    
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.and.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(171);
    }];
    
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(85);
        make.centerX.mas_equalTo(0);
        make.width.mas_equalTo(124);
        make.height.mas_equalTo(37);
    }];
    
    [loginBtn setNeedsDisplay];
    [loginBtn setNeedsLayout];
    loginBtn.layer.cornerRadius = 3;
    
    
    
    CGFloat picW = 77;
    [piciamge mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(53);
        make.centerX.mas_equalTo(0);
        make.width.mas_equalTo(picW);
        make.height.mas_equalTo(picW);
        
    }];
    
    piciamge.layer.cornerRadius = picW/2;
    piciamge.clipsToBounds = YES;
    
    [piciamge setNeedsDisplay];
    [piciamge setNeedsLayout];
    
    CGFloat nameLabelW = [namelabel intrinsicContentSize].width;
    CGFloat nameLabelh = [namelabel intrinsicContentSize].height;
    
    [namelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(0);
        make.top.equalTo(piciamge.mas_bottom).offset(6);
        make.width.mas_equalTo(nameLabelW);
        make.height.mas_equalTo(nameLabelh);
        
    }];
    
    CGFloat jifenlh = [namelabel intrinsicContentSize].height;
    
    [jifen mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(namelabel.mas_bottom).offset(6);
        make.left.mas_equalTo(0);
        make.width.equalTo(topView);
        make.height.mas_equalTo(jifenlh);
        
    }];
    
    [dingdanView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(topView.mas_bottom).offset(15);
        make.left.and.right.mas_equalTo(0);
        make.height.mas_equalTo(44);
        
    }];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.mas_equalTo(0);
        make.top.equalTo(dingdanView.mas_bottom).offset(0);
        make.height.mas_equalTo(15);
    }];
    
    
    
    
    CGFloat setW = [settingbtn intrinsicContentSize].width;
    [settingbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(26);
        make.right.mas_equalTo(-14);
        make.width.mas_equalTo(setW);
        make.height.mas_equalTo(22);
    }];
    
 
    UIView *lastView = nil;
    CGFloat  i = 0;
    for (ChyModel * model in _dataArr) {

        for ( HeadView * hd in model.headview) {
            UIView *subv = [UIView new];
            subv.tag = 100+i;
            subv.backgroundColor = [UIColor chycolorR:255 andG:255 andB:255];
            [plistView addSubview:subv];
            [_PlistViewArr addObject:subv];
            
            UIImageView * LeftimageView = nil;
            UILabel * labelText = nil;
            UIButton * button = nil;
            UILabel * linlabel = nil;
            UIImageView *  rightImage = nil;
            
            UILabel * redlabel = [[UILabel alloc]init];
            redlabel.font = [UIFont systemFontOfSize:10];
            redlabel.textColor = [UIColor chycolorR:255 andG:255 andB:255];
            redlabel.textAlignment = NSTextAlignmentCenter;
            
            if (redlabel.text.length<2) {
                redlabel.hidden = YES;
            }
            
            if (![hd.type integerValue] == 0) {
                LeftimageView = [self addimageView:@""];
                //            [[UIImageView alloc]init];
                LeftimageView.tag = 200+i;
                labelText = [self addtextLabel:@""];
                //            [[UILabel alloc]init];
                labelText.tag = 300+i;
                rightImage = [self addimageView:@"rightSnip"];
                redlabel.backgroundColor = [UIColor redColor];
                redlabel.tag = 400+i;
                linlabel = [[UILabel alloc]init];
                linlabel.backgroundColor = [UIColor chycolorR:232 andG:232 andB:232 ];
                button = [UIButton buttonWithType:(UIButtonTypeCustom)];
                button.tag = subv.tag;
            }
            [subv addSubview:LeftimageView];
            
            [subv addSubview:labelText];
            [subv addSubview:redlabel ];
            [subv addSubview:rightImage];
            [subv addSubview:button];
            
            [subv addSubview:linlabel];
            
            [button addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
            
            [subv mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.right.mas_equalTo(0);
                
                if ([hd.type integerValue ] == 0) {
                    make.height.mas_equalTo(10);
                    subv.backgroundColor = [UIColor chycolorR:232 andG:232 andB:232];
                }else {
                    make.height.mas_equalTo(48);
                }
                if ( lastView ) {
                    make.top.mas_equalTo(lastView.mas_bottom);
                }
                else {
                    make.top.mas_equalTo(plistView.mas_top);
                }
            }];
            
            [LeftimageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(subv);
                make.left.mas_equalTo(13);
                make.width.mas_equalTo(25);
                make.height.mas_equalTo(25);
                
            }];
            //
            CGFloat labelW = [labelText intrinsicContentSize].width;
            [labelText mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.centerY.equalTo(subv);
                make.left.equalTo(LeftimageView.mas_right).offset(10);
                make.width.mas_equalTo(labelW);
                make.height.mas_equalTo(20);
                //
            }];
            [rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(20);
                make.height.mas_equalTo(20);
                make.right.mas_equalTo(-11);
                make.centerY.mas_equalTo(0);
            }];
            [redlabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(0);
                make.width.mas_equalTo(20);
                make.height.mas_equalTo(20);
                if ([hd.type integerValue] == 1) {
                    make.right.equalTo(rightImage.mas_left).offset(-8);
                }
                
            }];
            
            
            [linlabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(subv.mas_bottom).offset(-0.5);
                make.left.equalTo(labelText.mas_left);
                make.right.mas_equalTo(0);
                make.height.mas_equalTo(1);
                
            }];
            
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(subv);
            }];
            lastView = subv;

            i++;
            
            
        }
    
    }
    [plistView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(contenView);
        make.top.equalTo(bgView.mas_bottom).offset(0);
                make.left.mas_equalTo(0);
                make.right.mas_equalTo(0);
                make.bottom.equalTo(lastView.mas_bottom);
                make.bottom.mas_equalTo(-10);
            }];
}

-(void)SetAddData{
    NSInteger i = 0;
    
    
    for (UIView * view in self.topArr) {
        
        
        
        
        if (view.tag == 10) {
            ChyModel * chymodel =  self.dataArr[i];
            
            NSLog(@"chymodel %@",chymodel.topView.pic);
            
            UIImageView * picImage = (UIImageView *)[view viewWithTag:1000];
            picImage.image = [UIImage imageNamed:chymodel.topView.pic];
            UILabel * namelabel  = (UILabel *)[view viewWithTag:1001];
            namelabel.text = [NSString stringWithFormat:@"%@", chymodel.topView.yonghu];
//            NSLog(@"name %@ ", namelabel.text);
            
            UILabel * Viplabel = (UILabel *)[view viewWithTag:1002];
            Viplabel.text = [NSString stringWithFormat:@"%@|%@",chymodel.topView.vip,chymodel.topView.inter];
                        NSLog(@"name %@  vip.text %@", namelabel.text , Viplabel.text);
            CGFloat namelabelW = [namelabel intrinsicContentSize].width;
            CGFloat namelabelH = [namelabel intrinsicContentSize].height;
            [namelabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(namelabelW);
                                make.height.mas_equalTo(namelabelH);
            
            }];
            CGFloat vipH = [Viplabel intrinsicContentSize].height;
            [Viplabel mas_updateConstraints:^(MASConstraintMaker *make) {
               
                make.height.mas_equalTo(vipH);
            }];
            
            if (chymodel.ISLogin ) {
                picImage.hidden = NO;
                namelabel.hidden = NO;
                Viplabel.hidden = NO;
            }else{
                picImage.hidden = YES;
                namelabel.hidden = YES;
                Viplabel.hidden = YES;
            }
            
        

            
            
            
        }
        else if (view.tag ==11)
        {
            ChyModel * chymodel =  self.dataArr[i];
            
            NSLog(@"chymodel %@",chymodel.topView.yonghu);
            
        }
    }
    
    
    
    
    for ( UIView  *hdView  in self.PlistViewArr) {
        
        for (ChyModel * model in self.dataArr) {
            
        HeadView * hd = model.headview[i];
  //                        左边的图片
            UIImageView * imageV = (UIImageView*)[hdView viewWithTag:200+i];
            
            imageV.image = [UIImage imageNamed:hd.image];
            //        title
            UILabel * label = (UILabel *)[hdView viewWithTag:300+i];
            label.text = [NSString stringWithFormat:@"%@" , hd.title];
//            小红点
            UILabel * redlabel = (UILabel *)[hdView viewWithTag:400+i];
            switch (i) {
                case 1: {
                    if (hd.number.length <= 2) {
                        redlabel.text = [NSString stringWithFormat:@"%@",hd.number];
                    }else{
                        redlabel.text = [NSString stringWithFormat:@"%@",@"99+"];
                    }
                    if (!model.ISLogin) {
                        redlabel.hidden = YES;
                    }else{
                        redlabel.hidden = NO;
                    }
                }
                    break;
                case 2:
                {
                    if (hd.number.length <= 2) {
                        redlabel.text = [NSString stringWithFormat:@"%@",hd.number];
                    }else{
                        redlabel.text = [NSString stringWithFormat:@"%@",@"99+"];
                    }
                    if (model.ISLogin) {
                        redlabel.hidden = NO;
                    }else{
                        redlabel.hidden = YES;
                    }
                }break;
                    
                case 3: {
                    if (hd.number.length <= 2) {
                        redlabel.text = [NSString stringWithFormat:@"%@",hd.number];
                    }else{
                        redlabel.text = [NSString stringWithFormat:@"%@",@"99+"];
                    }
                if ([redlabel.text isEqualToString:@""]||[redlabel.text isEqualToString:@"0"]) {
                        redlabel.hidden = YES;
                    }else{
                        
                        if (model.ISLogin) {
                            redlabel.hidden = NO;
                        }else{
                            
                            redlabel.hidden = NO;
                        }
                    }
                }
                    break;
                case 4:{
                    redlabel.text = [NSString stringWithFormat:@"%@",hd.number];
                    if (!model.ISLogin) {
                        redlabel.hidden = YES;
                    }else{
                        redlabel.hidden = NO;
                    }
                }break;
                    
                default:
                    break;
            }
            
            CGFloat LabelW = [label intrinsicContentSize].width;
            [label mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(LabelW);
            }];
            
            CGFloat Redlabelw = [redlabel intrinsicContentSize].width;
            
            if ([redlabel.text integerValue] <= 99) {
                redlabel.layer.cornerRadius = 10;
            }else if ([redlabel.text isEqualToString:@"99+"]){
                redlabel.layer.cornerRadius = 10;
            }else{
                
                [redlabel mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.width.mas_equalTo(Redlabelw);
                }];
                redlabel.layer.cornerRadius = 6;
                
            }
            redlabel.clipsToBounds = YES;
            
            
            i++;
        }
  }
}











-(UILabel *)addtextLabel:(NSString *)labeltext{
    UILabel * label = [[UILabel alloc]init];
    label.text = [NSString stringWithFormat:@"%@", labeltext];
    label.textColor = [UIColor chycolorR:34 andG:34 andB:34 ];
    label.font = [UIFont systemFontOfSize:14];
    return label;
}


-(UIImageView * )addimageView:(NSString *)imagename{
    UIImageView * imageview = [[UIImageView alloc]init];
    imageview.image = [UIImage imageNamed:imagename];
    return imageview;
}


-(void)btn:(UIButton*)sender{
    NSLog(@"sender");
    NSLog(@"sebder.tag: %zd" , sender.tag);
}

-(void)settingbtn:(UIButton*)sender{
    
    NSLog(@"设置");
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    if (scrollView.contentOffset.y < - 50) {
        [scrollView setContentOffset:CGPointMake(0, -50) animated:YES];
        [self scrollViewDidEndDragging:scrollView willDecelerate:YES];

    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (decelerate) {
        scrollView.contentOffset = CGPointMake(0, -50);
    }else{
        CGPoint offset = scrollView.contentOffset;
        offset.y = 0;
        scrollView.contentOffset = offset;
    }

}

@end
